package com.hthy.futures.ui.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.hthy.futures.AppContext;
import com.hthy.futures.R;
import com.hthy.futures.base.BaseFragment;
import com.hthy.futures.utils.TDevice;
import com.vilyever.socketclient.SocketClient;

import butterknife.Bind;

/**
   *  单据
   * Author:  Mr.bingley
   * Version:
   * Date:  2017/3/6
   */

/**
 * 发现了一个奇怪的bug,在basefragment中是没事的，在baselistfragment中是有事的bu'n
 */

public class BillSeachContainFragment extends BaseFragment  {
    @Bind(R.id.view_tab_layout)
    TabLayout mViewTabLayout;
  @Bind(R.id.view_pager)
    ViewPager mViewPager;

    BillSearchFragment mBillChildFragment ;
    BillSearchFragment mBillChildFragment2 ;
    BillSearchFragment mBillChildFragment3 ;
    BillSearchFragment mBillChildFragment4  ;
    private String[] mBillTitle;

    private Fragment mContent;

    @Override
    protected int getLayoutId() {
        return R.layout.frg_bill;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mBillTitle = getResources().getStringArray(R.array.array_search_billtitle);

     FragmentPagerAdapter mallPageAdapter = new TabFragmentAdapter(getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(mallPageAdapter);
        mViewTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(0);


        // 在tablayout间加入竖线
        LinearLayout linearLayout = (LinearLayout) mViewTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(getActivity(),
                R.drawable.layout_divider_vertical));
        // 分割线变短
        linearLayout.setDividerPadding(TDevice.dp2px(8));
    }


    public class TabFragmentAdapter extends FragmentPagerAdapter {

        public TabFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            switch (position) {
                case 0://
                    if (mBillChildFragment == null) {
                        mBillChildFragment = new BillSearchFragment();
                    }
                    bundle.putInt(getString(R.string.intent_key_type), 0);
                    mBillChildFragment.setArguments(bundle);
                    return mBillChildFragment;
                case 1://
                    if (mBillChildFragment2 == null) {
                        mBillChildFragment2 = new BillSearchFragment();
                    }
                    bundle.putInt(getString(R.string.intent_key_type), 1);
                    mBillChildFragment2.setArguments(bundle);
                    return mBillChildFragment2;
                case 2://
                    if (mBillChildFragment3 == null) {
                        mBillChildFragment3 = new BillSearchFragment();

                    }
                    bundle.putInt(getString(R.string.intent_key_type), 2);
                    mBillChildFragment3.setArguments(bundle);
                    return mBillChildFragment3;
                case 3://
                    if (mBillChildFragment4 == null) {
                        mBillChildFragment4 = new BillSearchFragment();

                    }
                    bundle.putInt(getString(R.string.intent_key_type), 3);
                    mBillChildFragment4.setArguments(bundle);
                    return mBillChildFragment4;
                default:
                    break;
            }
            return null;
        }

        @Override
        public int getCount() {
            if (mBillTitle != null) {
                return mBillTitle.length;
            }
            return 0;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return mBillTitle[position];
        }
    }

    public void switchContent(Fragment from, Fragment to) {
        if (mContent != to) {
            mContent = to;
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(from).add(R.id.lin, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }



}
